<?php

class PrimaryController extends Controller {

  public $content;

  public function init() {

    if (user()->getUserProperty('username') == '')
      $this->redirect(FRONT_SITE_URL);
  }

  public function actionIndex() {
    $this->render('index');
  }

  public function actionSendBirthdayFlirt() {
    $result = array('status' => 'Error',
        'message' => 'Whoops... something\'s amiss');

    $result['status'] = 'Success';
    echo CJSON::encode($result);
    Yii::app()->end();
  }

  public function actionSaveToFavorites() {
    $result = array('status' => 'Error',
        'message' => 'Whoops... something\'s amiss');

    if (r()->isPostRequest && isset($_POST['EncodedMemberId'])) {
      $mUserFavorite = new mUserFavorite();
      $checkExists = $mUserFavorite->find('user_id=:user_id and user_favorited=:user_favorited', array(':user_id' => user()->getUserProperty('user_id'), ':user_favorited' => $_POST['EncodedMemberId']));
      if ($checkExists == null) {
        $mUserFavorite->user_id = user()->getUserProperty('user_id');
        $mUserFavorite->user_favorited = $_POST['EncodedMemberId'];
        $mUserFavorite->date_favorite = date('Y-m-d');
        if ($mUserFavorite->save()) {
          $result['status'] = 'Success';
          $result['message'] = 'Saved to favorites!';
        }
      }else{
        $result['status'] = 'Success';
        $result['message'] = 'Saved to favorites!';
      }
        
    }
    echo CJSON::encode($result);
    Yii::app()->end();
  }

  public function actionUnfavorite() {
    $result = array('status' => 'Error',
        'message' => 'Whoops... something\'s amiss');
    if (r()->isPostRequest && isset($_POST['EncodedMemberId'])) {
      $mUserFavorite = new mUserFavorite();
      
      $checkExists = $mUserFavorite->find('user_id=:user_id and user_favorited=:user_favorited', array(':user_id' => user()->getUserProperty('user_id'), ':user_favorited' => $_POST['EncodedMemberId']));
      if ($checkExists != null) {
        if ($mUserFavorite->deleteAll('user_id=:user_id and user_favorited=:user_favorited', array(':user_id' => user()->getUserProperty('user_id'), ':user_favorited' => $_POST['EncodedMemberId'])) == 1) {
          $result['status'] = 'Success';
        }
      }else{
        $result['status'] = 'Success';
      }
    }

    echo CJSON::encode($result);
    Yii::app()->end();
  }
  
  public function actionSendMessage(){
    $result['status'] = 'Success';
    $purifier = new CHtmlPurifier();
    if(isset($_POST['EncodedMemberId']) && isset($_POST['Message'])){
      $mUserMessage = new mUserMessage();
      $mUserMessage->user_id = user()->getUserProperty('user_id');
      $mUserMessage->user_messaged = $purifier->purify($_POST['EncodedMemberId']);
      $mUserMessage->datetime_send = date('Y-m-d H:i:s');
      $mUserMessage->text = $purifier->purify($_POST['Message']);
      $mUserMessage->draft = 0;
      $mUserMessage->read = 1;
      $mUserMessage->feature_name = $purifier->purify($_POST['FeatureName']);
      $mUserMessage->from_page = $purifier->purify($_POST['fromPage']);
      $mUserMessage->save();
    }
    echo CJSON::encode($result);
    YII::app()->end();
  }
  
  public function actionSendMessageDetechNull(){
    $result['status'] = 'Success';
    $purifier = new CHtmlPurifier();
    if(isset($_POST['EncodedMemberId']) && isset($_POST['Message'])){
      $mUserMessage = new mUserMessage();
      $mUserMessage->user_id = user()->getUserProperty('user_id');
      $mUserMessage->user_messaged = $purifier->purify($_POST['EncodedMemberId']);
      $mUserMessage->datetime_send = date('Y-m-d H:i:s');
      $mUserMessage->text = $purifier->purify($_POST['Message']);
      $mUserMessage->draft = 0;
      $mUserMessage->read = 1;
      $mUserMessage->feature_name = $purifier->purify($_POST['FeatureName']);
      $mUserMessage->from_page = $purifier->purify($_POST['fromPage']);
      $mUserMessage->save();
    }
    echo CJSON::encode($result);
    YII::app()->end();
  }
  
  public function actionLikePhoto() {
    $result = array('status' => 'Error',
        'message' => 'Whoops... something\'s amiss');
    if (r()->isPostRequest && isset($_POST['EncodedMemberId'])) {
      $mUser = new User();
      $checkExists = $mUser->find('user_id=:user_id', array(':user_id' => $_POST['EncodedMemberId']));
      if ($checkExists != null) {
        $mPhotosLike = new mPhotosLike();
        $mPhotosLike->user_id = $_POST['EncodedMemberId'];
        $mPhotosLike->photos_id = $_POST['ObjectId'];
        $mPhotosLike->like = 1;
        $mPhotosLike->draft_like = 0;
        $mPhotosLike->primary_photos = 0;
        $mPhotosLike->viewed = 0;
        $mPhotosLike->text = '';
        if ($mPhotosLike->save()) {
          $result['status'] = 'Success';
        }
        $errores = $mPhotosLike->getErrors();
        var_dump($errores);
      }
    }
    $result['status'] = 'Success';
    echo CJSON::encode($result);
    Yii::app()->end();
  }
  
  public function actionRefreshSubHeaderCounts(){
    $strJson = '{"status":"Success","html":"\r\n\r\n            \u003cp class=\"\"\u003e\u003ca href=\"/v3/messages/?start=1\u0026amp;Folder=Inbox\u0026amp;sortcol=message\u0026amp;sortdir=DESC\u0026amp;upn=4\"\u003e\u003cspan class=\"headercountstotalvalue\"\u003eMessages\u003c/span\u003e\u003c/a\u003e\r\n            \u003c/p\u003e\r\n            \u003cp class=\"\"\u003e\u003ca href=\"/v3/connect/connectlist?tab=2\u0026amp;filter=1\"\u003e\u003cspan class=\"headercountstotalvalue\"\u003eViewed Me\u003c/span\u003e\u003c/a\u003e\r\n            \u003c/p\u003e\r\n            \u003cp class=\"\"\u003e\u003ca href=\"/v3/connect/connectlist?tab=1\u0026amp;filter=1\u0026amp;upn=5\"\u003e\u003cspan class=\"headercountstotalvalue\"\u003eFavorited Me\u003c/span\u003e\u003c/a\u003e\r\n            \u003c/p\u003e\r\n            \u003cp class=\"\"\u003e\u003ca href=\"/v3/messages/?start=1\u0026amp;Folder=Inbox\u0026amp;sortcol=flirt\u0026amp;sortdir=DESC\u0026amp;upn=6\"\u003e\u003cspan class=\"headercountstotalvalue\"\u003eFlirted With Me\u003c/span\u003e\u003c/a\u003e\r\n            \u003c/p\u003e\r\n            \u003cp class=\"\"\u003e\u003ca href=\"/v3/messages/?start=1\u0026amp;Folder=Inbox\u0026amp;sortcol=like\u0026amp;sortdir=DESC\u0026amp;upn=67\"\u003e\u003cspan class=\"headercountstotalvalue\"\u003eComment:\u0026nbsp;1\u0026nbsp;\u003c/span\u003e\u003c/a\u003e\r\n                \u003ca class=\"headercountsnewlink\" href=\"/v3/messages/?start=1\u0026amp;Folder=Inbox\u0026amp;sortcol=like\u0026amp;sortdir=DESC\u0026amp;upn=67\"\u003e(\u003cspan class=\"headercountsnewvalue\" \u003e1\u0026nbsp;New\u003c/span\u003e)\u003c/a\u003e\r\n            \u003c/p\u003e\r\n            \u003cp class=\"pnoborder\"\u003e\u003ca href=\"/v3/profile/todaysmatches\"\u003e\u003cspan class=\"headercountstotalvalue\"\u003eToday\u0026#39;s Matches\u003c/span\u003e\u003c/a\u003e\r\n                \u003ca class=\"headercountsnewlink\" href=\"/v3/profile/todaysmatches\"\u003e(\u003cspan class=\"headercountsnewvalue\" \u003e5\u0026nbsp;New\u003c/span\u003e)\u003c/a\u003e\r\n            \u003c/p\u003e\r\n","display":true}';
    echo $strJson;
    Yii::app()->end();
  }
}